package com.qf.springboot.controller;

import com.qf.springboot.utils.Constants;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
public class DownloadController {
    @GetMapping("/download")
    public void download(String filename, HttpServletRequest request, HttpServletResponse response) {
        String finalFilepath = Constants.UPLOAD_PATH + File.separator + filename;
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        try {
            IOUtils.copy(new FileInputStream(finalFilepath), response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
